package com.wzh.action.control.collect;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wzh.bean.PageView;
import com.wzh.bean.QueryResult;
import com.wzh.bean.collect.SiteInfo;
import com.wzh.service.siteinfo.SiteInfoService;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wzh.utils.BookCollectUtil;
import com.wzh.utils.HtmlJsoupUtil;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

// /\* [1-9 ]\d+ \*/
@Controller
public class CollectConfigAction extends ActionSupport {
    private static final long serialVersionUID = 1L;

    public static Map<String, String> urlcontent = new HashMap<String, String>();
    @Resource
    private SiteInfoService siteInfoSerive;
    private SiteInfo siteinfo;
    private int page = 1;
    private boolean isedit = false;

    public boolean isIsedit() {
        return this.isedit;
    }

    public void setIsedit(boolean isedit) {
        this.isedit = isedit;
    }

    public int getPage() {
        return this.page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public SiteInfo getSiteinfo() {
        return this.siteinfo;
    }

    public void setSiteinfo(SiteInfo siteinfo) {
        this.siteinfo = siteinfo;
    }

    public String index() {
        PageView pageView = new PageView(4, this.page);
        QueryResult qr = this.siteInfoSerive.getScrollData();
        pageView.setRecords(qr.getResultlist());
        pageView.setTotalrecords((int) qr.getTotalrecord());
        ActionContext.getContext().put("pageView", pageView);
        return "index";
    }

    public void addcheckreg_jsoup() {
        //获取相应Response
        HttpServletResponse response = ServletActionContext.getResponse();
        HttpServletRequest request = ServletActionContext.getRequest();
        //设置编码方式
        response.setCharacterEncoding("UTF-8");
        try {
            String url = request.getParameter("url");
            String reg = request.getParameter("reg");
            String charset = request.getParameter("charset");

            String content = urlcontent.get(url);
            if (content == null) {
                content = BookCollectUtil.getWebContentByUrl(url, charset);
                urlcontent.put(url, content);
            }//缓存

//                    String result = BookCollectUtil.getContentByReg(reg,content);
            String result = HtmlJsoupUtil.getBookUrl(content, null, null).toString();


            Map _result = new HashMap();
            _result.put("url", url);
            _result.put("reg", reg);
            _result.put("result", result);
            _result.put("content", content);
            response.getWriter().write(JSONObject.toJSONString(_result));
            //将数据写到页面中
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addcheckreg() {
        //获取相应Response
        HttpServletResponse response = ServletActionContext.getResponse();
        HttpServletRequest request = ServletActionContext.getRequest();
        //设置编码方式
        response.setCharacterEncoding("UTF-8");
        try {
            String key = request.getParameter("content");
            String url = request.getParameter("url");
            String reg = request.getParameter("reg");
            String charset = request.getParameter("charset");

            String content = urlcontent.get(url);
            if (content == null) {
                content = BookCollectUtil.getWebContentByUrl(url, charset);
                urlcontent.put(url, content);
            }
            String result = BookCollectUtil.getContentByReg_jsoup(reg, content, key);


            Map _result = new HashMap();
            _result.put("url", url);
            _result.put("reg", reg);
            _result.put("result", result);
            _result.put("content", content);
            response.getWriter().write(JSONObject.toJSONString(_result));
            //将数据写到页面中
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public String addSiteReg() {
        this.isedit = false;
        return "add";
    }

    public String insertSiteReg() {
        this.siteInfoSerive.save(this.siteinfo);
        ActionContext.getContext().put("urladdress", "/control/collect/index");
        ActionContext.getContext().put("message", "增加采集规则成功");
        return "message";
    }

    public String editSiteReg() {
        this.siteinfo = ((SiteInfo) this.siteInfoSerive.find(this.siteinfo.getId()));
        this.isedit = true;
        return "add";
    }

    public String updateSiteReg() {
        this.siteInfoSerive.update(this.siteinfo);
        ActionContext.getContext().put("urladdress", "/control/collect/index");
        ActionContext.getContext().put("message", "更新采集规则成功");
        return "message";
    }

    public String delete() {
        this.siteInfoSerive.delete(new Serializable[]{this.siteinfo.getId()});
        ActionContext.getContext().put("urladdress", "/control/collect/index");
        ActionContext.getContext().put("message", "删除采集规则成功");
        return "message";
    }
}

/* Location:           C:\work\apache-tomcat-7.0.55\webapps\ROOT\WEB-INF\classes\
 * Qualified Name:     com.wzh.action.control.collect.CollectConfigAction
 * JD-Core Version:    0.6.1
 */